// pages/info/edit/edit.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nickname: "",
    realName: "",
    phoneNum: "",
    IDNum: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _that=this
    if(wx.getStorageSync('role')=='user'){
      wx.request({
        url: 'http://localhost/Wx/Userinfo',
        method:'post',
        header:{
          "Content-Type": "application/x-www-form-urlencoded"
        },
        data:{
          openid:wx.getStorageSync('openid')
        },
        success(res){
          console.log(res.data.data)
          _that.setData({
            nickname:res.data.data.nickname,
            realName:res.data.data.realname,
            phoneNum:res.data.data.phone,
            IDNum:res.data.data.idcard,
          })
        }
      })
    }else if(wx.getStorageSync('role')=='manager'){
      wx.request({
        url: 'http://localhost/Wx/Managerinfo',
        method:'post',
        header:{
          "Content-Type": "application/x-www-form-urlencoded"
        },
        data:{
          openid:wx.getStorageSync('openid')
        },
        success(res){
          console.log(res.data.data)
          _that.setData({
            nickname:res.data.data.nickname,
            realName:res.data.data.realname,
            phoneNum:res.data.data.phone,
            IDNum:res.data.data.idcard,
          })
        }
      })
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  onSubmit: function () {
    let _that = this
    console.log(_that.data)
    if ((_that.data.IDNum != "") && (_that.data.nickname != "") && (_that.data.realName != "") && (_that.data.phoneNum != "")) {
      if(wx.getStorageSync('role')=='user'){
        wx.request({
          url: 'http://localhost/Wx/UpdateUserinfo',
          method: 'POST',
          header:{
            "Content-Type": "application/x-www-form-urlencoded"
          },
          data:{
            openid:wx.getStorageSync('openid'),
            nickname: _that.data.nickname,
            realname:_that.data.realName,
            phoneNum:_that.data.phoneNum,
            idcard: _that.data.IDNum
          },
          success(res){
            if(res.data.code==200){
              wx.showToast({
                title: res.data.msg,
                icon: 'succes',
                duration: 200000,
                mask:true,
                success:function(){
                  wx.reLaunch({
                    url: '/pages/my/my',
                  })
                }
              })
            }
          }
        })
      }else if(wx.getStorageSync('role')=='manager'){
        wx.request({
          url: 'http://localhost/Wx/UpdateManagerInfo',
          method: 'POST',
          header:{
            "Content-Type": "application/x-www-form-urlencoded"
          },
          data:{
            openid:wx.getStorageSync('openid'),
            nickname: _that.data.nickname,
            realname:_that.data.realName,
            phoneNum:_that.data.phoneNum,
            idcard: _that.data.IDNum
          },
          success(res){
            if(res.data.code==200){
              wx.showToast({
                title: res.data.msg,
                icon: 'succes',
                duration: 200000,
                mask:true,
                success:function(){
                  wx.reLaunch({
                    url: '/pages/my/my',
                  })
                }
              })
            }
          }
        })
      }
      
      
    } else {
      console.log("空信息")
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})